<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/26
 * Time: 16:09
 */
namespace app\admin\controller;

use app\admin\model\OrderModel;
use app\admin\controller\Product;
use app\admin\model\MemberModel;
use think\Db;


class Order extends Base
{
    /**
     * 添加订单
     * Array
     */
    public function orderAdd ()
    {
        $order = new OrderModel();
        if (request()->isPost()){
            $param = input('post.');
            if(!isset($param['cofigArr'])){
              return msg(-1, '', '请选择产品属性');
            }
			if(!isset($param['name'])){
              return msg(-1, '', '请选择产品');
            }
            $attr_arr = explode(",",explode("^",$param['cofigArr'])[0]);

            $price = explode("price:",$attr_arr[sizeof($attr_arr)-1])[1];
            unset($attr_arr[sizeof($attr_arr)-1]);
            $pid = [];
            foreach($attr_arr as $k => $v){
                $pid[explode('-',$v)[0]] = explode('-',$v)[1];
            }
            $param['grade'] = $pid[1];
            $param['system'] = $pid[2];
            $param['payment_method'] = $pid[3];
            $param['acquisition_mode'] = $pid[4];
            $param['total_amount'] = $price;
            $order_sn = date('Ymd',time()).rand(10000, 99999);
            $agent = session('id');
            $param['pro_id'] = (int)$param['name'];//订单来源
            $param['source'] = 2;//订单来源
            $param['order_sn'] = $order_sn;//收据号(订单号)
            $param['status'] = 1; //订单状态
            $param['c_time'] = time(); //创建时间
            $param['consultant'] = $param['relation_id']; //所属咨询师
            $param['create_uid'] = $agent; //创建人ID
            $param['attr_info'] = $param['cofigArr']; //产品属性信息

			//修改客户信息($where)OR添加客户信息
			if(isset($param['m_id'])){
				$where = [];
				$where['id'] = $param['m_id'];
				$where['name'] = $param['stu_name'];
				$where['phone'] = $param['phone'];
				$where['adress'] = $param['adress'];

				$where['status'] = 7;//状态
				$where['nation'] = $param['nation'];//民族
				$where['id_card'] = $param['id_card'];//身份证号
				$where['birthday'] = $param['birthday'];//生　　日
				// $where['sex'] = $param['sex'];//性　　别
				//录入信息
				$where['relation_id'] = $param['relation_id'];//关联人
				$where['time'] = $param['time'];//时间
				$memberModel = new MemberModel;
				$king = $memberModel->memberEdit($where);
				if ($king['code'] == 1) {
					$flag = $order->getOrderAdd($param);
		            return json(msg($flag['code'], $flag['data'], $flag['msg']));
				}else {
					return json(msg($king['code'], $king['data'], $king['msg']));
				}
			}else{
				$where = [];
				$where['name'] = $param['stu_name'];
				$where['phone'] = $param['phone'];
				$where['qq'] = $param['qq'];
				$where['weixin'] = $param['weixin'];
				$where['adress'] = $param['adress'];
				$where['remarks'] = $param['remarks'];

				$where['status'] = 7;//状态
				$where['grade'] = 2;//等级
				$where['nation'] = $param['nation'];//民族
				$where['id_card'] = $param['id_card'];//身份证号
				$where['birthday'] = $param['birthday'];//生　　日
				$where['sex'] = $param['sex'];//性　　别
				$where['create_uid'] = $param['relation_id'];//创建者id
				//录入信息
				$where['first_relation'] = $param['relation_id'];//关联人
				$where['entry_time'] = time();//时间

				$memberModel = new MemberModel;
				$memberId = $memberModel->addMember($where);
				$param['m_id'] = $memberId;
				$flag = $order->getOrderAdd($param);
				return json(msg($flag['code'], $flag['data'], $flag['msg']));
			}

        }
    }

    /**
     * Ajax 查询产品列表
     */
    public function productList()
    {
        if (request()->isAjax()){
            $param = input('param.');
            $school_id = $param['school_id'];
            $major_id = $param['major_id'];
            $order = new OrderModel();
            $proName = $order->getProductList($school_id,$major_id);
            return json($proName);
        }
    }
    /**
     * Ajax 查询产品列表
     */
    public function configList()
    {
        if (request()->isAjax()){
            $param = input('param.');
            $id = $param['product_id'];
            $order = new OrderModel();
            $cofigArr = $order->getConfigList($id);
            return ['code'=>1,'data'=>$cofigArr];
        }
    }
}
